import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter_kit_plus/flutter_kit.dart';

import '../../commont/button.dart';
import '../../model/model.dart';
import '../../tool/assets.dart';

class EqSetting extends StatefulWidget {
  bool up;
  List<SignModel> sign_list;
  bool enable;
  void Function(SignModel) onChangeForSend;
  void Function(SignModel, String) onChange;
  EqSetting(
      {super.key,
      this.up = true,
      required this.enable,
      required this.sign_list,
      required this.onChange,
      required this.onChangeForSend});

  @override
  State<EqSetting> createState() => _EqSettingState();
}

class _EqSettingState extends State<EqSetting> {
  SignModel get sign_select {
    return widget.sign_list.firstWhere(($0) => $0.select, orElse: () => widget.sign_list.first);
  }

  bool get enable {
    return widget.enable && sign_select.enable;
  }

//左右加减
  Widget _img_item(bool isAdd) {
    return SizedBox(
      width: 18 * GlobalConfig.scaleH,
      height: 18 * GlobalConfig.scaleH,
      child: imgButton(
          groundColor: Color_bg_view,
          enable: enable,
          img: Image.asset(isAdd ? assets.add : assets.sum, color: enable ? Colors.black : Color_disEnable),
          onClick: () {
            setState(() {
              if (widget.up) {
                sign_select.freq = isAdd ? sign_select.freq + 1 : sign_select.freq - 2;
                sign_select.freq = max(sign_select.freq, 20);
                sign_select.freq = min(sign_select.freq, 20000);
              } else {
                sign_select.Q = isAdd ? sign_select.Q + 1 : sign_select.Q - 2;
                sign_select.Q = max(sign_select.Q, sign_select.min_Q);
                sign_select.Q = min(sign_select.Q, sign_select.max_Q);
              }
            });
            sign_select.select = true;
            widget.onChange(sign_select, 'EQ');
          }),
    );
  }

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(builder: (BuildContext context, BoxConstraints constrains) {
      double w = (constrains.maxWidth - (widget.sign_list.length - 1) * 5) / widget.sign_list.length;
      return SizedBox(
          height: 70 * GlobalConfig.scaleH,
          child: Stack(
            children: [
              Positioned(
                left: 0,
                right: 0,
                top: widget.up ? 10 * GlobalConfig.scaleH : 40 * GlobalConfig.scaleH,
                height: 20 * GlobalConfig.scaleH,
                child: GridView.extent(
                  maxCrossAxisExtent: w,
                  mainAxisSpacing: 5,
                  crossAxisSpacing: 5,
                  childAspectRatio: w / 20 / GlobalConfig.scaleH,
                  physics: const NeverScrollableScrollPhysics(), // 禁止滚动
                  children: widget.sign_list.map(($0) {
                    bool sub_enable = widget.enable && $0.enable;
                    return kTextButton(
                      name: widget.up ? '${$0.freq}' : '${$0.Q}',
                      enable: sub_enable,
                      textColor: !sub_enable
                          ? Color_disEnable
                          : $0.select
                              ? Colors.white
                              : Colors.black,
                      borderColor: !sub_enable
                          ? Color_disEnable
                          : $0.select
                              ? Color_theme
                              : Colors.black,
                      groundColor: sub_enable && $0.select ? Color_theme : Color_bg_view,
                      onClick: (p0) {
                        setState(() {
                          $0.select = true;
                        });
                        widget.onChange($0, 'EQ');
                      },
                    );
                  }).toList(),
                ),
              ),

              ///中间设置freq 和 q 的地方
              Positioned(
                  left: 0,
                  right: 10,
                  top: widget.up ? 40 * GlobalConfig.scaleH : 10 * GlobalConfig.scaleH,
                  height: 20 * GlobalConfig.scaleH,
                  child: Row(
                    children: [
                      Container(
                        width: 80,
                        height: 20 * GlobalConfig.scaleH,
                        padding: const EdgeInsets.only(left: 5, right: 5),
                        decoration: BoxDecoration(
                            color: Color_bg_view,
                            borderRadius: BorderRadius.circular(4),
                            border: Border.all(color: enable ? Colors.black : Color_disEnable, width: 0.5)),
                        child: Row(
                          children: [
                            SizedBox(
                              width: 35,
                              child: Text(
                                widget.up ? 'Freq:' : 'Q:',
                                style: TextStyle(
                                    fontSize: 12,
                                    fontWeight: FontWeight.bold,
                                    color: enable ? Colors.black : Color_disEnable),
                              ),
                            ),
                            Center(
                              child: Text(
                                widget.up ? '${sign_select.freq}' : '${sign_select.Q}',
                                textAlign: TextAlign.right,
                                style: TextStyle(fontSize: 10, color: enable ? Colors.black : Color_disEnable),
                              ),
                            ),
                          ],
                        ),
                      ),

                      ///间隙
                      SizedBox(width: 10 * GlobalConfig.scaleH),
                      _img_item(false),
                      Expanded(
                          child: kSlider(
                        value: widget.up ? sign_select.freq.toDouble() : sign_select.Q,
                        min: widget.up ? 20 : sign_select.min_Q,
                        max: widget.up ? 20000 : sign_select.max_Q,
                        activeColor: Color_theme,
                        thumbColor: Color_theme,
                        enable: enable,
                        onChanged: (p0) {
                          setState(() {
                            sign_select.select = true;

                            if (widget.up) {
                              sign_select.freq = p0.roundDouble(1).toInt();
                            } else {
                              sign_select.Q = ((p0 * 100).toInt() / 100.0).roundDouble(1);
                            }
                          });
                          widget.onChange(sign_select, '');
                        },
                        onChangedForSend: (p0) {
                          widget.onChangeForSend(sign_select);
                        },
                      )),
                      _img_item(true),
                    ],
                  )),

              ///划线
              Positioned(left: 0, right: 0, bottom: 0, height: 0.5, child: Container(color: Color_border)),
            ],
          ));
    });
  }
}
